#include "srregisterpage.h"

#include <fstream>
#include <QFile>
#include <QSettings>
#include <QGridLayout>
#include <QDebug>
#include <QDateTime>

#include "subpage/SRRegisterPage"

#include <OECommonHelper>
#include <QLabel>
// private core library
#include <auditcore.h>
#include <auditcryptogram.h>

#include "srdefine.h"

SRRegisterPage::SRRegisterPage(int _registerMode, QWidget *parent)
    : QWidget(parent), registerMode_(_registerMode)/*,isRegister_(0)*/
{
    setWindowTitle(tr("复审员注册"));
    layout_ = new QVBoxLayout();

    SRCryptolaliaWidget* reg = new SRCryptolaliaWidget();
    reg->setRegisterMode(registerMode_);
    layout_->addWidget(reg,0, Qt::AlignTop);
    connect(reg,SIGNAL(sigCryptolaliaFinshed(const QString &,const QString &)),
            this, SLOT(onCryptolaliaFinished(const QString &,const QString &)));

    currentWidget_ = reg;

    setLayout(layout_);

}

SRRegisterPage::~SRRegisterPage()
{
//    if (isRegister_ == 0) {
//        apiResetAccount(Q_NULLPTR,"admin");
//    }
#ifdef QT_DEBUG
    qDebug() << "注册窗口被析构";
#endif
}


void SRRegisterPage::setCurrentWidget(QWidget *_wdgt) {
    if (currentWidget_) {
        layout_->removeWidget(currentWidget_);
        currentWidget_->disconnect();
        currentWidget_->deleteLater();
        currentWidget_ = Q_NULLPTR;
    }
    if (_wdgt != Q_NULLPTR)
        layout_->addWidget(_wdgt,0,Qt::AlignTop);

    currentWidget_ = _wdgt;
}

//void SRRegisterPage::onRegisterInfo(const QString &_name,
//                                    const QString &_user,
//                                    const QString &_pswd) {
//    hide();
//    // 对账号密码进行处理再加工
//    if (0 != apiRegisterAccount(_name.toUtf8().data(),
//                                _user.toUtf8().data(),
//                                _pswd.toUtf8().data())) {
//        // @remark : 注册失败
//        qDebug("注册失败");
//        return;
//    }

//    // 密语注册
//    SRCryptolaliaWidget* cry = new SRCryptolaliaWidget(this);
//    cry->setRegisterMode(registerMode_);
//    connect(cry,SIGNAL(sigCryptolaliaFinshed(const QString &,const QString &)),
//            this, SLOT(onCryptolaliaFinished(const QString &,const QString &)));
//    setCurrentWidget(cry);
//    showNormal();
//}

//void SRRegisterPage::onRegisterFinished(const QString& _supervisorPswd) {
//    hide();
//    // 对主管员密语进行处理再加工
//    if (0 != apiRegisterSupervisor(_supervisorPswd.toUtf8().data())) {
//        // @remark : 注册失败
//        qDebug("注册失败");
//        return;
//    }

//    // 清理密语注册页面的资源
//    setCurrentWidget(Q_NULLPTR);

//    // 注册完毕
//    isRegister_ = 1;


//    emit sigRegisterFinshed();
//    showNormal();
//}

void SRRegisterPage::onExportFinished()
{
    hide();
    // 清理密语注册页面的资源
    setCurrentWidget(Q_NULLPTR);
//    isRegister_ = 1;
    emit sigRegisterFinshed();
//    // 主管密码注册
//    SRSupervisorWidget* supervisor = new SRSupervisorWidget();
//    connect(supervisor,SIGNAL(sigPswdDone(const QString &)),
//            this, SLOT(onRegisterFinished(const QString &)));
//    setCurrentWidget(supervisor);
//    showNormal();
}


void SRRegisterPage::onCryptolaliaFinished(const QString &_cryptolalia,
                                          const QString &_key) {
    // 对密语进行处理再加工
    char ch_seed[256] = {};
    int ret = apiEncryptionCryptolalia(_cryptolalia.toUtf8().data(),0,
                                       ch_seed, _key.toUtf8().data());
    if (ret != 0) {
        qFatal("加密过程中遇到未知错误: "\
               "(SRRegisterPage.onCryptolaliaFinshed"\
               ".apiEncryptionCryptolalia)");

        // @remark: 引发未知错误注册失败提示框
        return;
    }

    char ch_prv_key[256] = {};
    char ch_pub_key[256] = {};
    apiGeneratedMainPrvKey(_cryptolalia.toUtf8().data(),
                           "2011/11/11",ch_prv_key);

    // save ekpubkey
    QString temp_wallet_config_path = walletPath_ + "/wallet";
    apiGeneratedPrvKeyInfo(ch_prv_key,ch_pub_key,0,0,0);
    QSettings setting(temp_wallet_config_path, QSettings::IniFormat);
    setting.setValue("INFO/ek_pub_key",ch_pub_key);
    setting.setValue("INFO/status", "未分配");
    setting.setValue("INFO/author", g_userName);
    setting.setValue("INFO/create_time",
            QDateTime::currentDateTime()
                .toString("yyyy-MM-dd hh:mm:ss"));
    ekPubKey_ = ch_pub_key;

    // save ekprvkey
#if 1

    QString temp_wallet_seed_path = walletPath_ + "/audit.sr.seed";
    QFile file(temp_wallet_seed_path);
    if(!file.open(QIODevice::WriteOnly))
    {
        qWarning("seed save failed, because open audit.sr.seed failed.");
    }
    file.write(ch_seed, sizeof(ch_seed));
    file.close();
#endif

    // 公钥导出
    SRExportWidget* exp = new SRExportWidget(ekPubKey_, registerMode_);

    connect(exp, &SRExportWidget::sigFinished,
            this, &SRRegisterPage::onExportFinished);
    connect(exp, &SRExportWidget::sigFinished,
            this, &SRRegisterPage::close);
    setCurrentWidget(exp);
//    qDebug() << parentWidget()->parentWidget();
    if (parentWidget()->parentWidget())
        parentWidget()->parentWidget()->setFixedHeight(500);

    return ;
}

// 因逻辑修改 此函数功能废弃
void SRRegisterPage::setRegisterMode(int _mode)
{
    registerMode_ = _mode;
}

void SRRegisterPage::setWalletPath(const QString &_path)
{
    walletPath_ = _path;
}
